package top.lixunda.ecommerce.server.good.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.common.utils.StringUtils;
import top.lixunda.ecommerce.server.business.BaseController;
import top.lixunda.ecommerce.server.business.api.entity.cache.CacheLoginUser;
import top.lixunda.ecommerce.server.business.api.entity.vo.ImageUploadTokenVO;
import top.lixunda.ecommerce.server.business.api.entity.dto.PageDTO;
import top.lixunda.ecommerce.server.business.api.entity.QiNiuCallbackBody;
import top.lixunda.ecommerce.server.good.api.controller.IFileCollectionController;
import top.lixunda.ecommerce.server.good.api.entity.db.FileCollectionDO;
import top.lixunda.ecommerce.server.good.api.entity.dto.FileCollectionQueryDTO;
import top.lixunda.ecommerce.server.good.api.entity.vo.FileCollectionVO;
import top.lixunda.ecommerce.server.good.service.IFileCollectionService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用于保存图片或文件，保存的图片或文件可以多次使用
 *
 * @author lixunda
 * @version time:2020/3/31 11:55
 */
@Slf4j
@RestController
public class FileCollectionController extends BaseController
        implements IFileCollectionController {

    private static final String UPLOAD_FILE_CALLBACK_URL =
            "http://lixunda.top:2100/middle-goods/file-collection/uploadFileCallback";

    private final IFileCollectionService fileCollectionService;

    @Autowired
    public FileCollectionController(IFileCollectionService fileCollectionService) {
        this.fileCollectionService = fileCollectionService;
    }

    /**
     * 获取当前用户保存的文件
     *
     * @return 文件数据列表
     */
    @Override
    public MessageObject<List<FileCollectionDO>> queryCollectionsByUser() {
        List<FileCollectionDO> list = fileCollectionService.queryCollectionsByUser();
        return getSuccessMessageObject("获取数据成功", list);
    }

    /**
     * 根据name获取当前用户保存的文件
     *
     * @param name name
     * @return 文件数据列表
     */
    @Override
    public MessageObject<List<FileCollectionDO>> queryCollectionsByUserWithName(String name) {
        List<FileCollectionDO> list = fileCollectionService.queryCollectionsByUserWithName(name);
        return getSuccessMessageObject("获取数据成功", list);
    }

    /**
     * 根据条件分页查询当前用户保存数据
     *
     * @param wrapper 分页查询参数
     * @return 分页数据
     */
    @Override
    public MessageObject<IPage<FileCollectionDO>> queryCollectionByUserWrapper(
            PageDTO<FileCollectionDO, FileCollectionQueryDTO> wrapper) {
        IPage<FileCollectionDO> page = fileCollectionService.queryCollectionByUserWrapper(wrapper);
        return getSuccessMessageObject("获取数据成功", page);
    }

    /**
     * 根据id移除数据，权限判断
     *
     * @param ids 编号列表
     * @return 是否移除成功
     */
    @Override
    public MessageObject<Boolean> removeByIds(List<Integer> ids) {
        boolean success = fileCollectionService.removeByIds(ids);
        return getSuccessMessageObject("移除数据成功", success);
    }

    /**
     * 获取一个上传图片的token
     *
     * @return ImageUploadTokenVO
     */
    @Override
    public MessageObject<ImageUploadTokenVO> createUploadToken() {
        CacheLoginUser cacheLoginUser = currentLoginUserCacheNotNull();
        ImageUploadTokenVO imageUploadTokenVO = createUploadToken(
                String.valueOf(cacheLoginUser.getAccount()), cacheLoginUser.getType(),
                UPLOAD_FILE_CALLBACK_URL, StringUtils.randomFileName(""));
        return getSuccessMessageObject("生成token成功", imageUploadTokenVO);
    }

    /**
     * 上传图片回调
     *
     * @param qiNiuCallbackBody 上传文件信息
     * @return 回调数据
     */
    @Override
    public MessageObject<FileCollectionVO> uploadFileCallback(QiNiuCallbackBody qiNiuCallbackBody) {
        FileCollectionVO fileCollectionVO = fileCollectionService.uploadFileCallback(qiNiuCallbackBody);
        return getSuccessMessageObject("上传图片成功", fileCollectionVO);
    }

    /**
     * 根据id下载文件，不推荐使用，推荐直接从七牛云进行下载
     *
     * @param id      id
     * @param request HttpServletRequest
     * @return ResponseEntity
     */
    @Override
    public ResponseEntity<byte[]> downloadAsFileById(Integer id, HttpServletRequest request) {
        return fileCollectionService.downloadAsFileById(id, request);
    }
}
